//
//  RefreshController.m
//  OCDemo
//
//  Created by BH on 15/10/15.
//  Copyright © 2015年 baohan. All rights reserved.
//

#import "RefreshController.h"
#import "RefreshModel.h"
#import "RefreshCell.h"
#import "HttpRequest.h"
#import <MJRefresh/MJRefresh.h>
#import <SDWebImage/SDImageCache.h>
#import <SDWebImage/UIImageView+WebCache.h>

@interface RefreshController ()<UITableViewDataSource,UITableViewDelegate,HttpRequestDelegate>
{
    NSInteger page;
    NSMutableArray *arrayM;
    HttpRequest *requset;
}

@end

@implementation RefreshController

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    arrayM = [[NSMutableArray alloc] init];
    
    self.table.tableFooterView = [[UIView alloc] init];
    self.table.rowHeight = 600;
    
    MJRefreshNormalHeader *refreshHeader = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshHeader)];
    [refreshHeader beginRefreshing];
    self.table.header = refreshHeader;
    refreshHeader.hidden = YES;
    
    MJRefreshAutoNormalFooter *refreshFooter = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshFooter)];
    self.table.footer = refreshFooter;
}

//下拉刷新
-(void)refreshHeader
{
    self.table.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        page = 1;
        [self requestData];
//        self.table.header.automaticallyChangeAlpha = YES;
    }];
    [self.table.header beginRefreshing];
}

//上拉加载
-(void)refreshFooter
{
    self.table.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        page++;
        [self requestData];
    }];
}

#pragma mark - tableview datasource & delegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return arrayM.count;
}

-(RefreshCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"refreshcell";
    RefreshCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"RefreshCell" owner:self options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    RefreshModel *model = arrayM[indexPath.row];
    cell.model = model;
    [cell.image sd_setImageWithURL:[NSURL URLWithString:model.wsding] placeholderImage:[UIImage imageNamed:@"placehod.jpeg"]];
    cell.name.text = model.name;
    cell.price.text = model.price;
    cell.district.text = model.district;
    cell.introduction.text = model.multipagetitle;
    cell.introduction.numberOfLines = 0;
    cell.priceOff.text = model.priceoff;
    cell.startTime.text = model.starttime;
    cell.endTime.text = model.endtime;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{}

#pragma mark - 网络请求
-(void)requestData
{
    requset = [[HttpRequest alloc] initWithPage:page delegate:self];
}

-(void)request:(HttpRequest *)request array:(NSMutableArray *)array
{
    [self.table.header endRefreshing];
    [self.table.footer endRefreshing];
    
    if (page == 1) {
        [arrayM removeAllObjects];
    }
    
    for (RefreshModel *model in array) {
        [arrayM addObject:model];
    }
    
    [self.table reloadData];
}

@end
